package ui;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.List;

public class Panel extends JPanel {
    private BufferedImage bg;
    private Plane plane;
    private List<EnemyPlane> enemyPlanes = new ArrayList<>();

    //创建一个新线程
    public void begin(){
        new Thread(){
            public void run(){
               while (true){
                   createEnemyPlane();//创建敌机
                   moveEnemyPlane();//敌机移动
                   repaint();
                   try {
                       Thread.sleep(20);
                   } catch (InterruptedException e) {
                       throw new RuntimeException(e);
                   }
               }
            }
        }.start();
    }

    private void moveEnemyPlane() {
        for(int i=0;i<enemyPlanes.size();i++){
            EnemyPlane enemyPlane = enemyPlanes.get(i);
            enemyPlane.y += 10;
        }
    }

    int index = 0;
    private void createEnemyPlane() {
        index++;
        if(index>=20)
        {EnemyPlane enemyPlane = new EnemyPlane();
        enemyPlanes.add(enemyPlane);
        index=0;}
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
//        g.drawString("今天天气好。",200,300);
        g.drawImage(bg,0,0,null);
        g.drawImage(plane.image,plane.x,plane.y,null);
        for(int i=0;i<enemyPlanes.size();i++){
            EnemyPlane enemyPlane = enemyPlanes.get(i);
            g.drawImage(enemyPlane.image,enemyPlane.x,enemyPlane.y,null);
        }

    }

    public Panel(Frame frame) {
        bg = GetImg.getImg("bg2.jpg");
        plane = new Plane();
        MouseAdapter mouseAdapter = new MouseAdapter() {
            @Override
            public void mouseMoved(MouseEvent e) {
                super.mouseMoved(e);
                plane.x = e.getX()-plane.image.getWidth()/2;
                plane.y = e.getY()-plane.image.getHeight()/2;
                repaint();
            }
        };
        addMouseListener(mouseAdapter);
        addMouseMotionListener(mouseAdapter);

        KeyAdapter keyAdapter = new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                super.keyPressed(e);
                int i = e.getKeyCode();
                if(i==KeyEvent.VK_A||i==KeyEvent.VK_LEFT){
                    plane.x = plane.x - 10;
                }
                else if(i==KeyEvent.VK_D||i==KeyEvent.VK_RIGHT){
                    plane.x = plane.x + 10;
                }
                else if(i==KeyEvent.VK_W||i==KeyEvent.VK_UP){
                    plane.y = plane.y - 10;
                }
                else if(i==KeyEvent.VK_S||i==KeyEvent.VK_DOWN){
                    plane.y = plane.y + 10;
                }
                repaint();
            }
        };
        frame.addKeyListener(keyAdapter);
    }
}
